import java.util.*;

/**
 * @author LKQ
 * @date 2022/6/5 9:25
 * @description 单调栈
 */
public class Solution {
    public static void main(String[] args) {

    }
    public int[] nextLargerNodes(ListNode head) {
        List<Integer> list = new ArrayList<>();
        while (head != null) {
            list.add(head.val);
            head = head.next;
        }
        int n = list.size();
        int[] ans = new int[n];
        Stack<Integer> stack = new Stack<>();
        for(int i = n - 1; i >= 0; i--) {
            int cur = list.get(i);
            while (!stack.isEmpty() && cur >= stack.peek()) {
                stack.pop();
            }
            ans[i] = stack.isEmpty() ? 0 : stack.peek();
            stack.push(cur);
        }
        return ans;
    }
}
